package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.elasticsearch.search.SearchService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

@Controller
public class SearchController {
    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;
    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;
    @Reference
    private ISearchService searchService;

    @RequestMapping("q")
    public String q(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_DESTINATION:
                return searchDestination(model,qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(model,qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravel(model,qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUser(model,qo);
            default:
                return searchAll(model,qo);
        }
    }
    //目的地查询
    private String searchDestination(Model model, SearchQueryObject qo) {


        DestinationTemplate destinationTemplate= destinationTemplateSearchService.findByName(qo.getKeyword());
        SearchResultVO data = new SearchResultVO();
        if(destinationTemplate == null){    //如果不存在,直接返回说找不到
            model.addAttribute("data",data);
        }else {  //如果存在,查询该目的地下所有的攻略、游记、用户,取前五条数据
            Long total = 0L;
            List<StrategyTemplate> sts = strategyTemplateSearchService.findByDestName(qo.getKeyword());
            total += sts.size();
            if(sts.size() > 5){
                sts.subList(0, 5);
            }
            List<TravelTemplate> tts = travelTemplateSearchService.findByDestName(qo.getKeyword());
            total += tts.size();
            if(tts.size() > 5){
                tts.subList(0, 5);
            }
            List<UserInfoTemplate> uts = userInfoTemplateSearchService.findByDestName(qo.getKeyword());
            total += uts.size();
            if(uts.size() > 5){
                uts.subList(0, 5);
            }
            data.setTotal(total);
            data.setStrategys(sts);
            data.setTravels(tts);
            data.setUsers(uts);
            model.addAttribute("data",data);
        }
        model.addAttribute("dest",destinationTemplate);
        return "index/searchDest";
    }
    //攻略查询
    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page",page);
        return "index/searchStrategy";
    }
    //游记查询
    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",page);
        return "index/searchTravel";
    }
    //用户查询
    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",page);
        return "index/searchUser";
    }
    //所有查询
    private String searchAll(Model model, SearchQueryObject qo) {
        //攻略的高亮查询
        Page<StrategyTemplate> strategyPage = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");
        //游记的高亮查询
        Page<TravelTemplate> travelPage = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        //用户的高亮查询
        Page<UserInfoTemplate> userPage = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        //目的地的高亮查询
        Page<DestinationTemplate> destPage = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME,
                DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");
        SearchResultVO data = new SearchResultVO();
        data.setStrategys(strategyPage.getContent());
        data.setTravels(travelPage.getContent());
        data.setUsers(userPage.getContent());
        data.setDests(destPage.getContent());
        data.setTotal(strategyPage.getTotalElements() + travelPage.getTotalElements()
                + userPage.getTotalElements() + destPage.getTotalElements());
        model.addAttribute("data",data);
        return "index/searchAll";
    }
}
